<template>

  <card-component
    :content-title="contentTitle"
    :page="page"
    @handleSizeChange="handleSizeChange"
    @handleCurrentChange="handleCurrentChange">

    <div slot="action">
      <pcbaRecordFilter @search="handleSearch" @exportExcel="handleExportExcel" @exportBomExcel="handleExportBomExcel"></pcbaRecordFilter>
<!--      <el-button type="primary" size="small" style="margin: 10px" @click="handleAddScanner">新增</el-button>-->
    </div>


    <table-component slot="table"
                     :table-header="tableHeader"
                     :table-data="tableData"
                     @handleDelete="handleDelete"
                     :page="page"></table-component>


  </card-component>

</template>

<script>

  import card from '@/base/card.vue'
  import table from '@/base/AutoWidthTable.vue'
  import action from '@/base/action.vue'
  import request from '@/common/js/request'
  import pcbaRecordFilter from '@/base/pcbaRecordFilter.vue'

  import util from '@/common/js/util'

  export default {
    components: {
      'card-component': card,
      'table-component': table,
      'action-componet': action,
      pcbaRecordFilter
    },
    data() {
      return {
        contentTitle: this.$getTextByLangKey("PCBA追溯"),
        tableHeader: [],
        tableData: [],
        page: {
          currentPage: 1,
          total: 0,
          pageSize: 20,
        },
        filterParams:{

        },

        timer: null,
        isRequesting: false,
        localAutoRequest : localStorage.getItem('autoRequest')
      }
    },
    created: function () {
      console.log('created')
      this.creatTimer()
    },
    destroyed: function () {
      this.stopTimer()
      console.log('---- destroyed ----')
    },

    watch: {

    },

    methods: {
      // requestPcbaRecordList,
      // requestPcbaRecordExport,
      // requestScanRecordListHistory
      // fetchPcbaRecordList(){
      //   request.requestPcbaRecordList({page:this.page.currentPage,size:this.page.pageSize,...this.filterParams},res=>{
      //     if(res.data.code === 200){
      //       const data = res.data.data
      //       this.tableHeader = this.makeHeader(data.headers)
      //       this.tableData = this.makeList(data.pageInfo.list)
      //       this.page.total = data.pageInfo.total
      //       console.log('data.pageInfo.total',data.pageInfo.total)
      //       this.page.currentPage = data.pageInfo.pageNum
      //     }
      //   })
      // },
      fetchDeletePcbaRecord(ids){
        request.requestDeletePcbaRecord({ids},res=>{
          if(res.data.code === 200){
            this.$message({showClose: true, message: '删除成功', type: 'success'});
            this.autoRequest()
          }
        })
      },
      fetchPcbaRecordExport(){
        request.requestPcbaRecordExport({...this.filterParams},res=>{
          if(res.data&&res.data.code === 200){
            this.$message({showClose: true, message: this.$getTextByLangKey("导出成功"), type: 'success'});
            return
          }
          this.$message.error(res.message)
        })
      },
      fetchPcbaRecordExportBom(){
          request.requestPcbaRecordExportBom({...this.filterParams},res=>{
          if(res.data&&res.data.code === 200){
            this.$confirm( `物料文件地址：${res.data.data}`,'导出成功', {
              confirmButtonText: '确定',
              showCancelButton: false,
              type: 'success'
            }).then(() => {

            }).catch(() => {

            });

            return
          }
          this.$message.error(res.message)
        })
      },


      makeHeader(headers){
        // let actionItem = {type: "action", label: this.$getTextByLangKey('操作'), isShow: true,width:'200px'}

        const newHeaders = headers?headers.map(x=>{

          if (x.prop === 'pcbaCode') {
            x.width = 200
            x.type = 'link'
          }
          if (x.prop === 'spiResult') {
            x.width = 120
            x.type = 'link'
          }
          if (x.prop === 'reflowTime') {
            x.type = 'link'
          }
          if (x.prop === 'smtResult') {
            x.width = 120
          }
          if (x.prop === 'aoiResult') {
            x.width = 120
          }
          if (x.prop === 'aoiRepairResult') {
            x.width = 120
          }

          if (x.prop === 'lineName') {
            x.width = 120
          }
          if (x.prop === 'createTime') {
            x.width = 150
          }
          if (x.prop === 'reflowTime') {
            x.width = 150
          }
          if (x.prop === 'smtTime') {
            x.width = 150
          }
          if (x.prop === 'aoiTime') {
            x.width = 150

          }if (x.prop === 'aoiRepairTime') {
            x.width = 150
          }
          x.isShow = true
          return x
        }):[]

        let actionItem = {type: "action", label: this.$getTextByLangKey('操作'), isShow: true,width:'100px'}
        newHeaders.push(actionItem)

        return newHeaders
      },

      makeList(list){
        const newList = list?list.map(x=>{
          x.createTime = x.createTime?new Date(x.createTime).format("yyyy/MM/dd hh:mm"):''
          x.reflowTime = x.reflowTime?new Date(x.reflowTime).format("yyyy/MM/dd hh:mm"):''
          x.smtTime = x.smtTime?new Date(x.smtTime).format("yyyy/MM/dd hh:mm"):''
          x.aoiTime = x.aoiTime?new Date(x.aoiTime).format("yyyy/MM/dd hh:mm"):''
          x.aoiRepairTime = x.aoiRepairTime?new Date(x.aoiRepairTime).format("yyyy/MM/dd hh:mm"):''
          x.pcbaCode_link = `./#/wrapper/scanHistoryList?srIds=${x.srIds}`;
          x.reflowTime_link = `./#/wrapper/reflowTempList?reflowTempId=${x.reflowTempId}`;
          x.spiResult_link = `./#/wrapper/spiDetailList?pcbaRecordId=${x.id}`;
          //reflowTempList id
          //spiDetailList pcbaRecordId
          x.action = 'delete'
          x.isShowHighLight = x.showHighLight
          return x
        }):[]
        return newList
      },

      handleSizeChange: function (val) {
        this.$emit("handleSizeChange", val)
      },
      handleCurrentChange: function (val) {
        this.page.currentPage = val
        this.autoRequest()
      },

      handleSearch(params){
        console.log('handleSearch',params)
        this.filterParams = params
        this.autoRequest()
      },
      handleExportExcel(params){
        this.filterParams = params
        this.autoRequest()
        this.fetchPcbaRecordExport()
      },
      handleExportBomExcel(params){
        this.filterParams = params
        this.autoRequest()
        this.fetchPcbaRecordExportBom()
      },
      handleDelete(index, row){
        console.log('row',row)
        this.fetchDeletePcbaRecord(row.id)
      },

      creatTimer: function () {
        this.autoRequest()
        if(this.$route.query.autoRequest === 'off' || this.localAutoRequest){
          console.log('关闭自动请求菜单')
          return
        }
        this.timer = setInterval(() => {
          if (!this.isRequesting) {
            this.autoRequest()
          } else {
          }
        }, 5000)
      },
      stopTimer: function () {
        clearTimeout(this.timer)
        this.isRequesting = false
      },

      autoRequest: function () {
        this.isRequesting = true
//        console.log('请求菜单栏')

        request.requestPcbaRecordList({page:this.page.currentPage,size:this.page.pageSize,...this.filterParams},res=>{
          this.isRequesting = false
          if(res.data.code === 200){
            const data = res.data.data
            this.tableHeader = this.makeHeader(data.headers)
            this.tableData = this.makeList(data.pageInfo.list)
            this.page.total = data.pageInfo.total
            console.log('data.pageInfo.total',data.pageInfo.total)
            this.page.currentPage = data.pageInfo.pageNum
          }
        },res=>{
          this.isRequesting = false
        })


        //
        // const data = res.data.data
        // this.tableHeader = this.makeHeader(data.headers)
        // this.tableData = this.makeList(data.pageInfo.list)

      },

    },


  }
</script>


<style scoped>
  .el-card {
    box-shadow: 0 0;
    border-radius: 0;
    border: none;
  }
</style>


